package com.letv.android.client.meditor;

import com.letv.android.client.fragment.AlbumRoomBaseControllerFragment.Style;
import com.letv.component.player.LetvMediaPlayerControl;

/**
 * videoView和videoController交互中介者
 * 
 * @author zhuqiao
 *
 */
public class VideoRoomControllerMeditor {

    /**
     * video模块控制controller模块的抽象接口
     *
     * @author zhuqiao
     *
     */
    public interface VideoToControllerListener {

        /**
         * 设置controller显示状态
         *
         * @param isForce 是否强制
         */
        void setControllerVisibility(int visibility, boolean isForce, Style style);

        /**
         * 设置seekbar进度
         *
         * @param max 总共
         * @param progress 当前进度
         * @param buffer
         */
        void initProcess(int max, int progress, int buffer);

        /**
         * 设置视频总时长
         *
         * @param time
         */
        void setTotalTime(int time);

        /**
         * 更新进度
         *
         * @param progress
         * @param buffer
         */
        void updateProgress(Style style, int progress, int buffer);

        /**
         * 开始
         */
        void start(boolean showController);

        /**
         * 暂停
         */
        void pause();

        /**
         * 设置是否可用
         *
         * @param enable
         */
        void setEnable(boolean enable);

        /**
         * 设置卡顿按钮是否可见
         *
         * @param show
         */
        void setBlockBtnVisibile(boolean show);

        /**
         * 设置seekbar是否可见
         *
         * @param show
         */
        void setSeekbarVisibile(boolean show);

        /**
         * 只有半屏有用
         */
        void initSeekBarBeginTextView();

        /**
         * 隐藏所有控制器layout
         */
        void hideAllControllerLayout();

        /**
         * 切换码流结束
         */
        void onStreamSwitchFinish();
    }

    /**
     * controller模块控制video模块的抽象接口
     *
     * @author zhuqiao
     *
     */
    public interface ControllerToVideoListener {

        LetvMediaPlayerControl getVideoView();

        /**
         * 停止拖动
         *
         * @param progress
         */
        void onSeekFinish(int progress);

        void buffTimeSchedule();

        /**
         * 开始刷新进度条
         */
        void startHandlerTime();

        /**
         * 停止刷新进度条
         */
        void stopHandlerTime();

        /**
         * 同步全半屏seekbar
         */
        void updateProgress(boolean fromFullScreen, int progress);

    }

    private final VideoToControllerListener mVideoToHalfControllerListener;
    private final VideoToControllerListener mVideoToFullControllerListener;
    private final ControllerToVideoListener mControllerToVideoListener;

    public VideoRoomControllerMeditor(VideoToControllerListener videoToHalfControllerListener,
                                      VideoToControllerListener videoToFullControllerListener, ControllerToVideoListener controllerToVideoListener) {
        mVideoToHalfControllerListener = videoToHalfControllerListener;
        mVideoToFullControllerListener = videoToFullControllerListener;
        mControllerToVideoListener = controllerToVideoListener;

        if (mVideoToHalfControllerListener == null || mVideoToFullControllerListener == null
                || mControllerToVideoListener == null) {
            throw new NullPointerException("VideoControllerMeditor param is null!");
        }
    }

    // ----------------- video控制controller -----------------

    /**
     * 设置controller的显示状态
     * 
     * @param isForce 是否强制显示或者隐藏
     */
    public void setControllerVisibility(int visibility, boolean isForce, Style style) {
        mVideoToHalfControllerListener.setControllerVisibility(visibility, isForce, style);
        mVideoToFullControllerListener.setControllerVisibility(visibility, isForce, style);
    }

    /**
     * 设置seekbar进度
     * 
     * @param max
     * @param progress
     * @param buffer
     */
    public void initProcess(int max, int progress, int buffer) {
        mVideoToHalfControllerListener.initProcess(max, progress, buffer);
        mVideoToFullControllerListener.initProcess(max, progress, buffer);
    }

    /**
     * 设置视频总时长
     * 
     * @param time
     */
    public void setTotalTime(int time) {
        mVideoToHalfControllerListener.setTotalTime(time);
        mVideoToFullControllerListener.setTotalTime(time);
    }

    /**
     * 更新进度
     * 
     * @param progress
     * @param buffer
     */
    public void updateProgress(Style style, int progress, int buffer) {
        mVideoToHalfControllerListener.updateProgress(style, progress, buffer);
        mVideoToFullControllerListener.updateProgress(style, progress, buffer);
    }

    /**
     * 设置是否可用
     * 
     * @param enable
     */
    public void setEnable(boolean enable) {
        mVideoToHalfControllerListener.setEnable(enable);
        mVideoToFullControllerListener.setEnable(enable);
    }

    /**
     * 设置卡顿按钮是否可见
     * 
     * @param show
     */
    public void setBlockBtnVisibile(boolean show) {
        mVideoToFullControllerListener.setBlockBtnVisibile(show);
    }

    /**
     * 设置seekbar是否可见
     * 
     * @param show
     */
    public void setSeekbarVisibile(boolean show) {
        mVideoToHalfControllerListener.setSeekbarVisibile(show);
        mVideoToFullControllerListener.setSeekbarVisibile(show);
    }

    /**
     * 开始
     */
    public void start(boolean showController) {
        mVideoToHalfControllerListener.start(showController);
        mVideoToFullControllerListener.start(showController);
    }

    /**
     * 暂停
     */
    public void pause() {
        mVideoToHalfControllerListener.pause();
        mVideoToFullControllerListener.pause();
    }

    public void initSeekBarBeginTextView() {
        mVideoToHalfControllerListener.initSeekBarBeginTextView();
    }

    public void hideAllControllerLayout() {
        // mVideoToHalfControllerListener.hideAllControllerLayout();
        // mVideoToFullControllerListener.hideAllControllerLayout();
    }

    public void onStreamSwitchFinish() {
        mVideoToHalfControllerListener.onStreamSwitchFinish();
        mVideoToFullControllerListener.onStreamSwitchFinish();
    }

    // ----------------- controller控制video -----------------
    public ControllerToVideoListener getControllerToVideoListener() {
        return mControllerToVideoListener;
    }
}
